package zhongbo.java.air.flightpath.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;
import zhongbo.java.air.common.vo.PageVo;
import zhongbo.java.air.common.vo.ResultVo;
import zhongbo.java.air.flightpath.listener.RedisLogChanelPublisher;
import zhongbo.java.air.flightpath.mapper.AirFlightPathMapper;
import zhongbo.java.air.flightpath.model.AirFlightPath;
import zhongbo.java.air.flightpath.service.IAirFlightPathService;

import java.util.Date;
import java.util.List;


@Service
public class AirFlightPathServiceImpl implements IAirFlightPathService {

    @Autowired
    private AirFlightPathMapper airFlightPathMapper;

    @Autowired
    private RedisLogChanelPublisher logChanelPublisher;

    @Override
    public String addAirFlightPath(AirFlightPath airFlightPath) {
        if (airFlightPath == null) {
            throw new RuntimeException("航迹记录对象为空");
        }
        isHasTheAddId(airFlightPath.getId());

        airFlightPath.setTimestamp(new Date());
        int insert = airFlightPathMapper.insert(airFlightPath);
        if (insert > 0) {
            logChanelPublisher.pubDoSomethingMessage("log_chanel","新增" + airFlightPath.getId() + " 数据 -在redis频道");
            return airFlightPath.getId();
        }
        throw new RuntimeException("新增航迹记录失败");
    }

    @Override
    public AirFlightPath updateAirFlightPath(AirFlightPath airFlightPath) {
        if (airFlightPath == null) {
            throw new RuntimeException("航迹对象为空");
        }
        airFlightPath.setTimestamp(new Date());

        int update = airFlightPathMapper.updateById(airFlightPath);
        if (update > 0) {
            logChanelPublisher.pubDoSomethingMessage("log_chanel","修改" + airFlightPath.getId() + " 数据 -在redis频道");
            return airFlightPath;
        }
        throw new RuntimeException("修改航迹记录失败");
    }

    @Override
    public int deleteAirFlightPath(String airId) {
        if (airId == null) {
            throw new RuntimeException("未找到航迹对象id");
        }

        int delete = airFlightPathMapper.deleteById(airId);
        if (delete > 0) {
            logChanelPublisher.pubDoSomethingMessage("log_chanel","删除" + airId + " 数据 -在redis频道");
            return delete;
        }
        throw new RuntimeException("删除航迹记录失败");
    }

    @Override
    public AirFlightPath getAirFlightPathById(String airId) {
        if (airId == null) {
            throw new RuntimeException("未找到航迹对象id");
        }
        return airFlightPathMapper.selectById(airId);
    }

    @Override
    public List<AirFlightPath> getAirFlightPathAll() {
        return airFlightPathMapper.getAirFlightPathAll();
    }

    @Override
    public PageVo<AirFlightPath> page(@RequestParam(required = false) String icaoCode,
                                      @RequestParam(defaultValue = "1") int pageNumber,
                                      @RequestParam(defaultValue = "1") int pageSize) {
        QueryWrapper<AirFlightPath> queryWrapper = new QueryWrapper<>();
        if (StrUtil.isNotBlank(icaoCode)) {
            queryWrapper.like("icao_code",icaoCode);
        }
        IPage pageParam = new Page(pageNumber,pageSize);
        IPage airFlightPage = airFlightPathMapper.selectPage(pageParam,queryWrapper);
        return new PageVo<>(airFlightPage);
    }

    @Override
    public PageVo<AirFlightPath> pageTimeFlightPathAll(String icao, Date startTime, Date endTime, Integer pageNumber, Integer pageSize) {
        QueryWrapper<AirFlightPath> queryWrapper = new QueryWrapper();
        if (StrUtil.isNotBlank(icao)) {
            queryWrapper.like("icao_code",icao).between("timestamp",startTime,endTime);
        }
        IPage pageParam = new Page(pageNumber,pageSize);
        IPage airIngestion = airFlightPathMapper.selectPage(pageParam,queryWrapper);
        return new PageVo<>(airIngestion);
    }

    public boolean isHasTheAddId(String airId) {
        List<AirFlightPath> allAir = airFlightPathMapper.getAirFlightPathAll();
        return allAir.stream().noneMatch(air -> air.getId().equals(airId));
    }
}
